//
//  KXLoginViewController.swift
//  KXBook
//
//  Created by  on 2022/9/8.
//

import UIKit

class KXLoginViewController: KXViewController {
    @IBOutlet var theme: UILabel!
    @IBOutlet var input1: KXTextField!
    @IBOutlet var input2: KXTextField!
    @IBOutlet var input3: KXTextField!
    @IBOutlet var loginBtn: UIButton!
    @IBOutlet var registerBtn: UIButton!
    @IBOutlet var selectBtn: UIButton!
    @IBOutlet var selectTop: NSLayoutConstraint!
    @IBOutlet var privateBtn: UIButton!
    var isLogin = true
    override func viewDidLoad() {
        super.viewDidLoad()
        self.input3.isHidden = self.isLogin
        self.selectTop.constant = self.isLogin ? -58 : 10
        self.registerBtn.isHidden = !self.isLogin
        self.loginBtn.setTitle(self.isLogin ? LS("登录") : LS("注册"), for: .normal)
        self.theme.text = self.isLogin ? LS("欢迎登录") : LS("欢迎注册")
        self.backBtn?.setImage(UIImage(named:"arrow_backward"), for: .normal)
        var attri = NSMutableAttributedString(string:LS("同意"),attributes: [.foregroundColor:UIColor.black,.font:font(12)])
        let attr2 = NSAttributedString(string:LS("《用户协议》&《隐私条款》"),attributes: [.foregroundColor:"#E27BC1".color(),.font:font(12)])
        attri.append(attr2)
        self.privateBtn.setAttributedTitle(attri, for: .normal)
    }
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        switch sender.tag{
        case 201:
            sender.isSelected = !sender.isSelected
        case 202:
            let vc = KXWebViewController()
            self.push(vc, true)
        case 203:
            if self.input1.text!.count <= 0{
                return Toast.msg(LS("账号不能为空"))
            }else if self.input2.text!.count <= 0{
                return Toast.msg(LS("密码不能为空"))
            }else if self.input1.text!.isNumberOrAlphabet() == false{
                return Toast.msg(LS("账号只能包含数字和字母"))
            }else if self.input2.text!.isNumberOrAlphabet() == false{
                return Toast.msg(LS("密码只能包含数字和字母"))
            }else if self.isLogin == false && self.input3.text!.count <= 0{
                return Toast.msg(LS("密码只能包含数字和字母"))
            }else if self.isLogin == false && self.input2.text != self.input3.text!{
                return Toast.msg(LS("两次密码不一致"))
            }else if self.selectBtn.isSelected == false{
                return Toast.msg(LS("请仔细阅读用户协议和隐私条款"))
            }
            self.request()
        case 204:
            let vc = KXLoginViewController()
            vc.isLogin = false
            self.push(vc, true)
            break
        default:
            break
        }
    }
    
    func request()
    {
        Toast.startHud()
        let url = self.isLogin ? KXConst.api_login : KXConst.api_register
        network.post(url, para: ["userName":self.input1.text!, "password":self.input2.text!]) { response in
            if self.isLogin == false{
                Toast.stopHud()
                let vc = self.navigationController?.viewControllers[self.navigationController!.viewControllers.count - 2] as! KXLoginViewController
                vc.input1.text = self.input1.text!
                vc.input2.text = self.input2.text!
                self.backBtnAction()
            }else{
                let token = (response as! NSDictionary).value(forKey: "data") as! NSString
                network.http().header = ["Authorization":token]
                network.get(KXConst.api_userinfo, para: nil, header: nil) { response in
                    let dict = (response as! NSDictionary).value(forKey:"data") as! NSDictionary
                    let user = User.mj_object(withKeyValues: dict)
                    user?.token = token as String
                    APP.user = user!
                    APP.user.bg_saveOrUpdate()
                    Toast.stopHud()
                    NotifyTool.post(NotifyName.updateUser)
                    self.backBtnAction()
                } fail: { error in}
                
            }
        } fail: { error in}
        
    }
}
